package com.xlh.support.quiz.dao.helper;

import com.xlh.dto.quiz.EssayQuestionStaticsDTO;
import com.xlh.dto.quiz.TrueOrFalseQuestionStaticsDTO;
import com.xlh.dto.quiz.UserQuizStatusDTO;
import com.xlh.support.quiz.dao.QuizAnswerMapper;
import com.xlh.support.quiz.domain.QuizAnswer;
import com.xlh.support.quiz.domain.example.QuizAnswerExample;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cheer
 */
@Repository
public class QuizAnswerMapperHelper {

    @Autowired
    private QuizAnswerMapper quizAnswerMapper;

    public void deleteByQuizId(Long quizId) {
        QuizAnswerExample example = new QuizAnswerExample();
        example.or().andQuizIdEqualTo(quizId)
                .andDeletedEqualTo(false);
        QuizAnswer answer = QuizAnswer.builder()
                .deleted(true)
                .build();
        quizAnswerMapper.updateByExampleSelective(answer, example);
    }

    // 真删
    public void deleteByQuizIds(List<Long> quizIds) {
        if (CollectionUtils.isNotEmpty(quizIds)) {
            QuizAnswerExample quizAnswerExample = new QuizAnswerExample();
            quizAnswerExample.or().andQuizIdIn(quizIds);
            quizAnswerMapper.deleteByExample(quizAnswerExample);
        }
    }

    public QuizAnswer getByUserIdAndQuizId(Long userId, Long quizId, Integer quizNumber) {
        QuizAnswerExample example = new QuizAnswerExample();
        example.or().andUserIdEqualTo(userId)
                .andQuizIdEqualTo(quizId)
                .andQuizNumberEqualTo(quizNumber)
                .andDeletedEqualTo(false);
        List<QuizAnswer> answers = quizAnswerMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(answers)) {
            return answers.get(0);
        }
        return null;
    }

    public List<QuizAnswer> listByQuizIds(List<Long> quizIds) {
        if (CollectionUtils.isEmpty(quizIds)) {
            return new ArrayList<>();
        }
        QuizAnswerExample example = new QuizAnswerExample();
        example.or().andQuizIdIn(quizIds)
                .andDeletedEqualTo(false);
        return quizAnswerMapper.selectByExample(example);
    }

    public List<QuizAnswer> listByQuizIdAndNumber(Long quizId, Integer quizNumber) {
        QuizAnswerExample example = new QuizAnswerExample();
        example.or().andQuizIdEqualTo(quizId)
                .andQuizNumberEqualTo(quizNumber)
                .andDeletedEqualTo(false);
        return quizAnswerMapper.selectByExample(example);
    }

    public List<QuizAnswer> listByQuizIdsAndUserIds(List<Long> quizIds, List<Long> userIds) {
        QuizAnswerExample example = new QuizAnswerExample();
        example.or().andUserIdIn(userIds)
                .andQuizIdIn(quizIds)
                .andDeletedEqualTo(false);
        return quizAnswerMapper.selectByExample(example);
    }

    public EssayQuestionStaticsDTO essayQuestionStatistic(Long chapterId, List<Long> userIds) {

        return quizAnswerMapper.essayQuestionStatistic(chapterId, userIds);
    }

    public TrueOrFalseQuestionStaticsDTO trueOrFalseQuestionsStatistic(Long chapterId, List<Long> userIds) {

        return quizAnswerMapper.trueOrFalseQuestionsStatistic(chapterId, userIds);
    }


    public List<UserQuizStatusDTO> findQuizStatisticByUserIds(List<Long> chapterIds, List<Long> userIds) {

        return quizAnswerMapper.findQuizStatisticByUserIds( chapterIds, userIds);
    }
}
